BaseFSM = BaseClass("BaseFSM")

function BaseFSM:__init()
    self.states = {}
    self.currentState = nil
end

function BaseFSM:AddState(fsmState)
    if fsmState == nil or fsmState.stateName == nil then
        return false
    end

    if self.states[fsmState.stateName] ~= nil then
        return false
    end

    self.states[fsmState.stateName] = fsmState
    return true
end

function BaseFSM:StartState(stateName)
    local startState = self.states[stateName]
    if startState == nil then
        return false
    end

    self.currentState = startState
    self.currentState:OnEnter()

    return true
end

function BaseFSM:GetCurrentState()
    return self.currentState
end

function BaseFSM:ChangeState(stateName)
    local targetState = self.states[stateName]
    if targetState == nil then
        return false
    end

    if self.currentState == targetState then
        return false
    end

    if not targetState:CheckEntryCondition() then
        return false
    end

    self.currentState:OnExit()
    self.currentState = targetState
    self.currentState:OnEnter()

    return true
end

function BaseFSM:Update()
    if self.currentState ~= nil then
        self.currentState:OnUpdate()
    end
end
